<?php
class CmsImagesController extends AppController {
    var $prefix  = 'cus_';
    var $imgPath = IMAGES;
    var $maxFilesize = 4096000;
    var $allowMimes  = array('image/jpeg', 'image/pjpeg', 'image/gif', 'image/png');

    function get ($filename=null) {
        $pos  = strrpos($filename, '.');
        $name = substr($filename, 0, $pos);
        $ext  = substr($filename, $pos);

        $match = glob(WWW_ROOT.'/img/'.$this->prefix.$name.'*'.$ext);
        if (count($match) > 0) {
            return $this->base.'/img/'.basename($match[0]);
        }

        $match = glob(WWW_ROOT.'/themed/'.$this->theme.'/img/'.$this->prefix.$name.'*'.$ext);
        if (count($match) > 0) { // use custom image in themed
            return $this->base.'/themed/'.$this->theme.'/img/'.basename($match[0]);
        }

        if (file_exists(WWW_ROOT.'/themed/'.$this->theme.'/img/'.$filename)) { // use original image in themed
            return $this->base.'/themed/'.$this->theme.'/img/'.$filename;
        }

        // use original image
        return $this->base.'/img/'.$filename;
    } // get

    function restore ($tag=null) {
        Configure::write('debug', 0);

        $def = basename($this->params['form']['current']);
        $len = strlen($this->prefix);

        if (substr($def, 0, $len) != $this->prefix) { // is not custom image
            $json = array(
                'status'  => -1,
                'message' => __('No any custom image found in server.',true)
            );
        } else {
            $match = glob(WWW_ROOT.'/themed/'.$this->theme.'/img/'.$tag.'.*');
            if (count($match) > 0) { // original image is found in themed
                $original = $this->base.'/themed/'.$this->theme.'/img/'.basename($match[0]);
            } else {
                $match = glob(WWW_ROOT.'/img/'.$tag.'.*');
                if (count($match) > 0) { // original image is found
                    $original = $this->base.'/img/'.basename($match[0]);
                }
            }

            if ($original) {
                @unlink($this->imgPath.'/'.$def);
                $json = array(
                    'status'   => 1,
                    'original' => $original,
                    'message'  => __('Default image is restored successfully!',true)
                );
            } else {
                $json = array(
                    'status'  => -1,
                    'message' => __('Restore failure, default image not found..',true)
                );
            }
        }

        header('X-JSON: { status:'.$json['status'].' }');
        $this->set('json', $json);
        $this->render(false, 'json');
    } // restore

    function upload () {
        Configure::write('debug', 0);

        $cfg = array(
            'uploadPath' => IMAGES.'/tmp',
            'minWidth'   => 50,
            'minHeight'  => 50
        );
        $form = $this->params['form'];
        $itag = $form['itag'];
        $file = $form['image_'.$itag];
        $form = Set::remove($form, 'image_'.$itag);

        extract($form);

        $json = $form;
        $temp = basename($file['tmp_name']);
        $def  = pathinfo($defaultImage, PATHINFO_FILENAME);
        $ext  = pathinfo($defaultImage, PATHINFO_EXTENSION);

        App::import('Vendor', 'Upload', array('file'=>'image.class.php'));
        $img = new Upload($file);

        $img->file_max_size      = $this->maxFilesize;
        $img->allowed            = $this->allowMimes;
        $img->image_min_width    = $cfg['minWidth'];
        $img->image_min_height   = $cfg['minHeight'];
        $img->file_new_name_body = $this->prefix.$itag.'_'.time();
         $img->file_overwrite     = true;
         $img->file_auto_rename   = false;
         $img->image_convert      = strtolower($ext);
         $img->image_resize       = true;
         $img->image_x            = $iWidth;
         $img->image_y            = $iHeight;
         $img->image_ratio        = true;
         $img->Process($this->imgPath);

        if ($img->processed) {
            $json['status']   = 1;
            $json['original'] = $file['name'];
            $json['filename'] = $img->file_dst_name;
            $json['message']  = __('image is uploaded successfully!',true);
            $img->Clean();

            $len = strlen($this->prefix);
            if (substr($def, 0, $len) == $this->prefix) { // already has this prefix?
                @unlink($this->imgPath.'/'.basename($defaultImage));
            }
        } else {
            $json['status']   = -1;
            $json['original'] = $file['name'];
            $json['filename'] = false;
            $json['message']  = __($img->error,true);
        }

        header('X-JSON: { status:'.$json['status'].' }');
        $this->set('json', $json);
        $this->render(false, 'json');
    } // upload
} // CmsImagesController
?>